package utils

import (
	"bytes"
	"strings"
	
	"github.com/yuin/goldmark"
)

func RenderMarkdown(md string) string {
	var buf bytes.Buffer
	if err := goldmark.Convert([]byte(md), &buf); err != nil {
		return md
	}
	return buf.String()
}

func GenerateSlug(title string) string {
	slug := strings.ToLower(title)
	slug = strings.ReplaceAll(slug, " ", "-")
	slug = strings.ReplaceAll(slug, "'", "")
	slug = strings.ReplaceAll(slug, "\"", "")
	return slug
}

func GenerateExcerpt(content string, length int) string {
	// 移除Markdown标记
	plain := content
	plain = strings.ReplaceAll(plain, "#", "")
	plain = strings.ReplaceAll(plain, "*", "")
	plain = strings.ReplaceAll(plain, "_", "")
	plain = strings.ReplaceAll(plain, "`", "")
	plain = strings.ReplaceAll(plain, "~", "")
	
	if len(plain) > length {
		return plain[:length] + "..."
	}
	return plain
}